package cn.iocoder.yudao.module.project.service.weighthistory;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.weighthistory.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.weighthistory.WeightHistoryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.weighthistory.WeightHistoryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 称重业务 Service 实现类
 *
 * @author 武汉华诚
 */
@Service
@Validated
public class WeightHistoryServiceImpl implements WeightHistoryService {

    @Resource
    private WeightHistoryMapper weightHistoryMapper;

    @Resource
    private PermissionApi permissionApi;

    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public String createWeightHistory(WeightHistorySaveReqVO createReqVO) {
        // 插入
        WeightHistoryDO weightHistory = BeanUtils.toBean(createReqVO, WeightHistoryDO.class);
        weightHistoryMapper.insert(weightHistory);
        // 返回
        return weightHistory.getId();
    }

    @Override
    public void updateWeightHistory(WeightHistorySaveReqVO updateReqVO) {
        // 校验存在
        validateWeightHistoryExists(updateReqVO.getId());
        // 更新
        WeightHistoryDO updateObj = BeanUtils.toBean(updateReqVO, WeightHistoryDO.class);
        weightHistoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteWeightHistory(String id) {
        // 校验存在
        validateWeightHistoryExists(id);
        // 删除
        weightHistoryMapper.deleteById(id);
    }

    private void validateWeightHistoryExists(String id) {
        if (weightHistoryMapper.selectById(id) == null) {
            throw exception(WEIGHT_HISTORY_NOT_EXISTS);
        }
    }

    @Override
    public WeightHistoryDO getWeightHistory(String id) {
        return weightHistoryMapper.selectById(id);
    }

    @Override
    public PageResult<WeightHistoryDO> getWeightHistoryPage(WeightHistoryPageReqVO pageReqVO) {
        return weightHistoryMapper.selectPage(pageReqVO);
    }




    @Override
    public List<WeightHistoryDO> selectSavedList(Map<String, Object> params) {
        List<WeightHistoryDO> list = weightHistoryMapper.selectSavedList(params);
        return list;
    }

    @Override
    public void insertRecord(List<WeightHistoryDO> insertList) {
        // 插入
        if (null != insertList && insertList.size() > 0) {
            weightHistoryMapper.insertList(insertList);
        }
    }

    @Override
    public void updateRecord(List<WeightHistoryDO> updateList) {
        // 更新
        if (null != updateList && updateList.size() > 0) {
            LocalDateTime today = LocalDateTimeUtil.now();
            for (WeightHistoryDO data : updateList) {
                data.setUpdateTime(today);
            }
            weightHistoryMapper.updateList(updateList);
        }
    }

    @Override
    public void saveRecord(List<WeightHistoryDO> dataList) {

    }

    @Override
    public List<WeightHistoryReportVO> selectWeightHistory(Map<String, Object> params) {
        Long loginUserId = WebFrameworkUtils.getLoginUserId();
        boolean commonFlag = permissionApi.hasAnyRoles(loginUserId, "longyi_common");
        boolean adminFlag = permissionApi.hasAnyRoles(loginUserId, "longyi_admin");
        boolean managerFlag = permissionApi.hasAnyRoles(loginUserId, "longyi_manager");

        AdminUserRespDTO adminUserRespDTO = adminUserApi.getUser(loginUserId);
        Long deptId = adminUserRespDTO.getDeptId();

        params.put("deptId", deptId);
        params.put("userId", loginUserId);
        if(commonFlag){
            params.put("role", "common");
        } else if (adminFlag) {
            params.put("role", "admin");
        }else if (managerFlag){
            params.put("role", "manager");
        }else {
            params.put("role", "other");
        }


        List<WeightHistoryReportVO> weightHistoryReportVOS = weightHistoryMapper.selectWeightHistory(params);
        return weightHistoryReportVOS;
    }

    @Override
    public Long countWeightHistory(Map<String, Object> params) {
        Long count = weightHistoryMapper.countWeightHistory(params);
        return count;
    }

}